package Linklist;

/**
 * @Classname JZ06从尾到头打印链表
 * @Description TODO
 * @Date 2023/2/9 21:08
 * @Created by xjl
 */
public class JZ06从尾到头打印链表 {

    class ListNode{
        int val;
        ListNode next;
        public ListNode(int val){
            this.val=val;
        }
    }

    // 链表的翻转
    public int[] reversePrint(ListNode head) {
        ListNode pre=null;
        ListNode curr=head;
        int count=0;
        while (curr!=null){
            ListNode next=curr.next;
            curr.next=pre;
            pre=curr;
            curr=next;
            count++;
        }
        // 翻转完成 然后遍历一次
        ListNode index=pre;
        int[] result=new int[count];
        int start=0;
        while (index!=null){
            result[start++]=index.val;
            index=index.next;
        }
        return result;
    }

    //  链表的翻转与遍历
    public ListNode reverse(ListNode head){
        ListNode pre=null;
        ListNode curr=head;
        while (curr!=null){
            ListNode next=curr.next;
            curr.next=pre;
            pre=curr;
            curr=next;
        }
        return pre;
    }
}
